const fs = require("fs");
const moment = require("moment");
const _ = require("underscore");
const bodyParser = require('body-parser');
const multer = require('multer');

const express = require("express");
const app = express();
//http请求的body解析器(适用于POST请求)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());

//加载session模块
const session = require("express-session");
const cookieParser = require("cookie-parser");
app.use(cookieParser());
app.use(session({
	secret: '12345',
	name: 'testapp',   //这里的name值的是cookie的name，默认cookie的name是：connect.sid
	cookie: {maxAge: 30*60*1000 },  //Cookie过期时间
	resave: false,
	saveUninitialized: true
}));


const addRoute = require("./controller/AddRoute");
//日期格式化语言设置
moment.locale("zh-cn");
var locals = {
	//全局作用域(相当于是ServletContext)中的属性
	moment : moment,
	formatPattern : {
		DATE : "LL",
		DATETIME : "LLL"
	}
};
_.extendOwn(app.locals, locals);

//读取配置文件
var config = require("./statics/LoadConfig");
if(!config) {
	return;
}
//静态资源目录
app.use(express.static(__dirname + config.resource));

//视图目录, 默认views目录
app.set("views", config.views);

//模板引擎, 默认ejs
app.set("view engine", config.view_engine);

//加载视图路由
addRoute(app);

var port = config.port;
//启动HTTP监听
app.listen(port, 511, function(){
	console.log(`HTTP请求监听 : 端口${port}`);
});

//创建WebSocket服务端
require("./service/WebSocketService").createServer();
